package com.gserver.commons.manager.async;

import java.util.TimerTask;

import com.gserver.netty.domain.Message;

public class ScheduledMessage extends Message{

	public static final int MESSAGE_STATE_INITIALIZED = 0;
	public static final int MESSAGE_STATE_WAITING = 1;
	public static final int MESSAGE_STATE_INQUEUE = 2;
	public static final int MESSAGE_STATE_CANCELED = 3;
	public static final int MESSAGE_STATE_EXECUTING = 4;
	public static final int MESSAGE_STATE_DONE = 5;
	private TimerTask future;
	private int state;
	private final long createTimestamp;
	private long trigerTimestamp;

	public int getInitBufferLength() {
		return 0;
	}

	public int getLength() {
		return 0;
	}

	
	public ScheduledMessage(long createTime) {
		setState(0);
		this.createTimestamp = createTime;
	}

	public long getTrigerTimestamp() {
		return this.trigerTimestamp;
	}

	public void setTrigerTimestamp(long finishTimestamp) {
		this.trigerTimestamp = finishTimestamp;
	}

	public synchronized int getState() {
		return this.state;
	}

	public synchronized boolean isCanceled() {
		return this.state == 3;
	}

	public synchronized void setState(int state) {
		this.state = state;
	}

	public TimerTask getFuture() {
		return this.future;
	}

	public void setFuture(TimerTask future) {
		this.future = future;
	}

	public synchronized void cancel() {
		if (this.state == 1) {
			this.future.cancel();
			setState(3);
		} else if (this.state == 2) {
			setState(3);
		}
	}

	public long getCreateTimestamp() {
		return this.createTimestamp;
	}

}
